package com.bdzl.auth.service.impl;

import com.bdzl.auth.common.utils.MD5Utils;
import com.bdzl.auth.dao.UserDao;
import com.bdzl.auth.service.UserRoleService;
import com.bdzl.auth.service.UserService;
import com.bdzl.pub.entity.sys.UserDO;
import com.bdzl.pub.entity.sys.UserRoleDO;
import com.bdzl.pub.util.file.BASE64DecodedMultipartFile;
import com.bdzl.pub.util.file.OssUploadFileUtil;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.sys.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;



@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserRoleService userRoleService;

	@Override
	public UserDO get(Integer userId){
		return userDao.get(userId);
	}

	@Override
	public List<UserDO> list(Map<String, Object> map){
		return userDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return userDao.count(map);
	}

	@Override
	public int save(UserDO user){
		return userDao.save(user);
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 保存用户以及保存用户角色关系表
	 * @Date 15:31 2021/5/20
	 * @Param [user]
	 * @return com.bdzl.pub.util.param.R
	 **/
	@Override
	public R saveUserDO(UserVO user){
		UserDO byUserLgname = userDao.getByUserLgname(user.getUserLgname());
		if (byUserLgname!=null){
			return R.error("登录账号重复");
		}
		//如果头像不为空 不以http开头
		if (user.getUserHeadImg()!=null && !user.getUserHeadImg().startsWith("http")){
			try {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(user.getUserHeadImg());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "loan/user/user_head ");
				//修改头像
				user.setUserHeadImg(docUrl);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//是否是客户 0：是； 1：不是
		user.setUserCustFlag("1");
		//用户是否首次登录 0：是； 1：不是
		user.setUserFirstLg("0");
		//加密初始密码
		String encrypt = MD5Utils.encrypt(user.getUserLgname(), "123456");
		user.setUserPwd(encrypt);
		List<Integer> roleList = user.getRoleList();
		if(userDao.save(user)>0){
			UserDO userDO = userDao.getByUserLgname(user.getUserLgname());
			//角色列表
			if (roleList!=null){
				if (roleList.size()>1){
					for (Integer roleId:roleList) {
						if (roleId==3){
							return R.error("业务经理不能和其他角色同时赋予一个账号！");
						}
					}
				}
				//删除原有角色列表
				userRoleService.removeByUserId(userDO.getUserId());
				//添加新的角色列表
				for (Integer roleId:roleList) {
					UserRoleDO userRoleDO = new UserRoleDO();
					userRoleDO.setRoleId(roleId);
					userRoleDO.setUserId(userDO.getUserId());
					userRoleService.save(userRoleDO);
				}
			}
			return R.ok(userDO);
		}
		return R.error();
	}

	@Override
	public int update(UserDO user){
		return userDao.update(user);
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 修改用户以及用户角色关系表
	 * @Date 15:31 2021/5/20
	 * @Param [user]
	 * @return com.bdzl.pub.util.param.R
	 **/
	@Override
	@Transactional
	public R updateUserDO(UserVO user){
		//如果头像不为空 不以http开头
		if (user.getUserHeadImg()!=null && !user.getUserHeadImg().startsWith("http")){
			try {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(user.getUserHeadImg());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "loan/user/user_head ");
				//修改头像
				user.setUserHeadImg(docUrl);
				//获取旧头像
				String userHeadImg = userDao.get(user.getUserId()).getUserHeadImg();
				if (userHeadImg!=null && userHeadImg.startsWith("http")){
					OssUploadFileUtil.remove(userHeadImg);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//角色列表
		if (user.getRoleList()!=null){
			if (user.getRoleList().size()>1){
				for (Integer roleId:user.getRoleList()) {
					if (roleId==3){
						return R.error("业务经理不能和其他角色同时赋予一个账号！");
					}
				}
			}
			//删除原有角色列表
			userRoleService.removeByUserId(user.getUserId());
			//添加新的角色列表
			List<Integer> roleList = user.getRoleList();
			for (Integer roleId:roleList) {
				UserRoleDO userRoleDO = new UserRoleDO();
				userRoleDO.setRoleId(roleId);
				userRoleDO.setUserId(user.getUserId());
				userRoleService.save(userRoleDO);
			}
		}
		userDao.update(user);
		UserDO userDO = userDao.get(user.getUserId());
		return R.ok(userDO);
	}

	@Override
	public int remove(Integer userId){
		userRoleService.removeByUserId(userId);
		return userDao.remove(userId);
	}

	@Override
	public int batchRemove(Integer[] userIds){
		return userDao.batchRemove(userIds);
	}

	@Override
	public R checkUserIsEffective(UserDO user) {
		//情况1：根据用户信息查询，该用户不存在
		if (user == null) {
			return R.error("该用户不存在，请注册");
		}
		//情况2：根据用户信息查询，用户状态 0：正常； 1：禁用
		if ("1".equals(user.getUserStatus())) {
			return R.error("该用户已禁用！");
		}
		return R.ok();
	}

	@Override
	public UserDO getByUserLgname(String userLgname) {
		return userDao.getByUserLgname(userLgname);
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 根据小程序openid获取用户
	 * @Date 14:02 2021/5/11
	 * @Param [userOpenid]
	 * @return com.bdzl.pub.entity.sys.UserDO
	 **/
	@Override
	public List<UserDO> getByUserOpenid(String userOpenid) {
		return userDao.getByUserOpenid(userOpenid);
	}

	@Override
	public List<UserDO> getHomeVisit() {
		return userDao.getHomeVisit();
	}

}
